class Solution {
public:
    int maxFrequency(vector<int>& nums, int k) {
        sort(nums.begin(),nums.end());
        long long sum=0;
        int ans=1;
        for(int left=0,right=1;right<nums.size();++right)
        {
            sum+=(long long)(nums[right]-nums[right-1])*(right-left);
            while(left<right&&sum>k)
            {
                sum-=nums[right]-nums[left++];
            }
            ans=max(ans,right-left+1);
        }
        return ans;
    }
};
